package com.zhangfei;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PdfReader {

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static void main(String[] args) {
        try {
            // 加载PDF文档
            PDDocument document = PDDocument.load(new File("C:\\Users\\滨湖华为\\Desktop\\books\\2024年04月信用卡账单.pdf"));

            // 创建PDFTextStripper对象
            PDFTextStripper pdfStripper = new PDFTextStripper();

            // 读取文本内容
            String text = pdfStripper.getText(document);
            // 使用正则表达式 \r?\n 匹配换行符，包括 \n 和 \r\n
            String[] lines = text.split("\\r?\\n");

            // 打印内容
            // 打印分割后的每一行
            System.out.println("打印分割后的每一行");

            List<String> huanKuanList=new ArrayList<>();

            for (String line : lines) {
                if (line.contains("(CN)")) {
                    String[] parts = line.split("\\s+");
                   //[ 04/01,  04/07,  支付宝-莆田市卡鳄贸易有限公司, -127.30, 7520, -127.30(CN)]

                    String replace = StringUtils.replace(parts[0], " ", "");
                    String transactionTimeOrigin ="2024-"+ replace.replace("/", "-");
                    System.out.println(transactionTimeOrigin);
                    System.out.println(parts[0].length());


                    String tallyTimeOrigin ="2024-"+  parts[1].trim().replace("/", "-");
//                    LocalDate transactionTime = LocalDate.parse(transactionTimeOrigin, DATE_FORMATTER);
//                    LocalDate tallyTime = LocalDate.parse(tallyTimeOrigin, DATE_FORMATTER);

//                    System.out.println(transactionTime);
//                    System.out.println(tallyTime);
                    System.out.println(parts[2].trim());




                }
            }





            // 关闭文档
            document.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
